<?php

namespace App\Service\delivery;
use App\Model\Applet;
use App\Model\AppletPlayletRel;
use App\Model\DataSpread;
use App\Model\Payment;
use App\Model\Playlet;
use App\Model\Company;
use App\Model\AppletCompany;
use App\Model\PlayletEpisodes;
use App\Model\RechargeGoods;
use App\Model\RechargeTemplate;
use App\Model\SystemOrderDay;
use App\Service\CommicService;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
use Hyperf\DbConnection\Db;

class StatisticsService extends CommicService
{
    const cacheDuration = 60 * 15;
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
        $this->userAppletIds = $this->getUserAppletInfo();
    }


    const STATUS_ENABLE  = 1;


    public function spreadSearch(Array $params,$export=0){

        $pageSize       = $params['pageSize'] ?? 10;
        $page           = $params['page'] ?? 1;
        $applet_ids     = $params['applet_ids'] ?? '';
        $data_spread_id = $params['data_spread_id'] ?? '';
//        $ad_id = $params['ad_id'] ?? '';
        $date     = $params['date'] ?? '';
        if(empty($applet_ids)){
            return [];
        }
        $userInfo = $this->getUserAppletInfo(2);
        //管理员能看到所有
        if($userInfo['role']['is_admin'] != 1){
            $spreadIds = DataSpread::query()->where(['user_id' => $userInfo['id']])->pluck('id');
        }

        $selectArr = ['date','data_spread_id',
            Db::raw('SUM(order_pay_count) as order_count'),
            Db::raw('SUM(order_wait_pay_count) as order_wait_pay_count'),
            Db::raw('SUM(order_pay_count) as order_pay_count'),
            Db::raw('SUM(order_cannel_count) as order_cannel_count'),
            Db::raw('SUM(order_return_count) as order_return_count'),
            Db::raw('SUM(order_wx_pay_count) as order_wx_pay_count'),
            Db::raw('SUM(order_virtually_pay_count) as order_virtually_pay_count'),
            Db::raw('SUM(first_user_count) as first_user_count'),
            Db::raw('SUM(first_user_pay_amonut) as first_user_pay_amonut'),
            Db::raw('SUM(order_wx_pay_amonut+order_virtually_pay_amount) as order_amount'),
            Db::raw('SUM(order_wx_pay_amonut) as order_wx_pay_amonut'),
            Db::raw('SUM(order_virtually_pay_amount) as order_virtually_pay_amount'),
        ];

        $groupBy = ['date','data_spread_id'];

        //获取所拥有的
        $query = SystemOrderDay::query()->select($selectArr);
        if($data_spread_id != ''){
            $query->where(['data_spread_id' => $data_spread_id]);

        }
//        if($ad_id != ''){
//            $query->where(['ad_id' => $ad_id]);
//        }
        if(!empty($spreadIds)){
            $query->whereIn('data_spread_id',$spreadIds);
        }
        if(!empty($date)){
            $date[0] = date('Y-m-d',$date[0]/1000);
            $date[1] = date('Y-m-d',$date[1]/1000);
            $query->whereBetween('date',$date);
        }
        $query->groupBy($groupBy);

        $models = $query->orderBy('date','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();
            if(!empty($item->spread)){
                $data['spread_name']  = $item->spread->name;
            }else{
                $data['spread_name']  = '';
            }
            $data['first_user_pay_amonut']  = sprintf("%.2f",floatval($data['first_user_pay_amonut']/100));
            $data['order_wx_pay_amonut']  = sprintf("%.2f",floatval($data['order_wx_pay_amonut']/100));
            $data['order_virtually_pay_amount']  =sprintf("%.2f",floatval($data['order_virtually_pay_amount']/100));
            $data['order_amount']  = sprintf("%.2f",floatval($data['order_wx_pay_amonut']+$data['order_virtually_pay_amount']));
            $selectData[] =$data;
        }
        $data = $models->toArray();
        $data['data'] = $selectData;
        return $data;
    }

    public function orderSearch(Array $params,$export=0){

        $pageSize       = $params['pageSize'] ?? 10;
        $page           = $params['page'] ?? 1;
        $applet_ids     = $params['applet_ids'] ?? '';
//        $data_spread_id = $params['data_spread_id'] ?? '';
        $date     = $params['date'] ?? '';
        if(empty($applet_ids)){
            return [];
        }
        $userInfo = $this->getUserAppletInfo(2);
        //管理员能看到所有
        if($userInfo['role']['is_admin'] != 1){
            $spreadIds = DataSpread::query()->where(['user_id' => $userInfo['id']])->pluck('id');
        }


        $selectArr = ['date',
            Db::raw('SUM(order_pay_count) as order_count'),
            Db::raw('SUM(order_wait_pay_count) as order_wait_pay_count'),
            Db::raw('SUM(order_pay_count) as order_pay_count'),
            Db::raw('SUM(order_cannel_count) as order_cannel_count'),
            Db::raw('SUM(order_return_count) as order_return_count'),
            Db::raw('SUM(order_wx_pay_count) as order_wx_pay_count'),
            Db::raw('SUM(order_virtually_pay_count) as order_virtually_pay_count'),
            Db::raw('SUM(first_user_count) as first_user_count'),
            Db::raw('SUM(first_user_pay_amonut) as first_user_pay_amonut'),
//            Db::raw('SUM(order_virtually_pay_count+order_wx_pay_amonut) as order_amount'),
            Db::raw('SUM(order_wx_pay_amonut) as order_wx_pay_amonut'),
            Db::raw('SUM(order_virtually_pay_amount) as order_virtually_pay_amount'),
        ];
        //获取所拥有的
        $query = SystemOrderDay::query()->select($selectArr);
//        var_dump();
//        if($data_spread_id != ''){
//            $query->where(['data_spread_id' => $data_spread_id]);
//        }
        if(!empty($spreadIds)){
            $query->whereIn('data_spread_id',$spreadIds);
        }
        if(!empty($date)){
            $date[0] = date('Y-m-d',$date[0]/1000);
            $date[1] = date('Y-m-d',$date[1]/1000);
            $query->whereBetween('date',$date);
        }
        $query->groupBy(['date']);


        $models = $query->orderBy('date','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();
            $data['first_user_pay_amonut']  = sprintf("%.2f",floatval($data['first_user_pay_amonut']/100));

            $data['order_wx_pay_amonut']  = sprintf("%.2f",floatval($data['order_wx_pay_amonut']/100));
            $data['order_virtually_pay_amount']  =sprintf("%.2f",floatval($data['order_virtually_pay_amount']/100));
            $data['order_amount']  = sprintf("%.2f",floatval($data['order_wx_pay_amonut']+$data['order_virtually_pay_amount']));
            $selectData[] =$data;
        }
        $data = $models->toArray();
        $data['data'] = $selectData;
        return $data;
    }

}